package servlet.admin;


import entity.Admins;
import service.AdminsService;
import service.Impl.AdminsServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(urlPatterns = "/admin/AdminLogin")
public class AdminLoginServlet extends HttpServlet {
    private AdminsService service = new AdminsServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        String adminName = req.getParameter("adminName");
        String adminPwd = req.getParameter("adminPwd");
        Admins admin = service.login(adminName, adminPwd);
        if (admin != null) {
            req.getSession().setAttribute("admin", admin);
            resp.sendRedirect(req.getContextPath() + "/admin/AdminsList");
        } else {

            req.getRequestDispatcher("/admin/login.jsp").forward(req, resp);
            req.setAttribute("msg", "用户名或密码错误");

        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req,resp);
        }
}

